import axios,{InternalAxiosRequestConfig,AxiosResponse} from 'axios'
// import {useUserStore} from "@/store/modules/user.ts";
import {ElMessage, ElMessageBox} from "element-plus";
import { TOKEN_KEY } from "@/enums/CacheEnum";
// const userStore = useUserStore()
// import { storeToRefs } from 'pinia'
// const token = userStore.token
// 创建axios实例
const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout:50000,
    headers:{"Content-Type":"application/json;charset=utf-8"}
})

//请求拦截器
service.interceptors.request.use(
    (config:InternalAxiosRequestConfig)=>{
        const accessToken = localStorage.getItem(TOKEN_KEY)
        if(accessToken){
            config.headers['Authorization'] = accessToken
        }
        return config
    },
(error: any) => {
    return Promise.reject(error);
}
)

// 响应拦截器
service.interceptors.response.use(
    (response:AxiosResponse)=>{
            const {code,msg} = response.data
        if (code === 200) {
            return response.data;
        }
        ElMessage.error(msg || '系统出错');
        return Promise.reject(new Error(msg || 'Error'));
    },
(error) =>{
        if(error.response.data){
            const { code, msg } = error.response.data;
            // token 过期，跳转登录页
            if (code === 401) {
                ElMessageBox.confirm('当前页面已失效，请重新登录', '提示', {
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                    localStorage.clear(); // @vueuse/core 自动导入
                    window.location.href = '/';
                });
            }else{
                ElMessage.error(msg || '系统出错');
            }

        }
    return Promise.reject(error.message);
}

)

// 导出 axios 实例
export default service;